<%--
  Created by IntelliJ IDEA.
  User: dzw
  Date: 2017/12/14
  Time: 15:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
  String _id=request.getParameter("_");
%>
<p id="cKeys" style="display: none">${cKeys}</p>
<div class="jqGrid_wrapper">
  <table id="dialog_form_table"></table>
  <div id="dialog_form_page"></div>
</div>

<script>

  var sFormMap = [
    <c:forEach var="item" items="${sFormMap}" varStatus="status">
    {"id":"${item.key}","name":"${item.value}"} <c:if test="${!status.last}">,</c:if>
    </c:forEach>
  ];

  $(function(){
    init_grid_dialog_form();
    $("#cb_dialog_form_table").hide();
  });

  function init_grid_dialog_form() {

    __init_jqgrid('dialog_form_table', 'dialog_form_page',
            '<%=path%>/com/startExam/getFormList?moduleName='+'${moduleName}',
            ['模块', 'id'],
            [
              {name: 'name', index: 'name', width: 60, sortable: true, stype: 'text', searchoptions: {sopt: ['cn']}},
              {name: 'id', index: 'id', width: 100, key: true, hidden: true, hidedlg: true}
            ],
            true,
            {
              multiselect: true,
              autowidth: false,
              width: 558,
              onSelectRow: function (rowId, status) {
                if(status){
                  setSelectValue();
                } else {
                  deletetSelectValue(rowId);
                }
              },
              gridComplete: function(){
                var sids = $("#cKeys").text();
                if(sids){
                  var arr = sids.split(",");
                  for(var i=0; i<arr.length; i++){
                    $(this).jqGrid("setSelection",arr[i]);
                  }
                }
              }
            }
    )

  }

  function setSelectValue(){
    var rowId = $("#dialog_form_table").jqGrid('getGridParam', 'selrow');
    var value = "";
    var selectedValue = $("#cKeys").text();
    if(selectedValue != "" && selectedValue != null){
      if(selectedValue.indexOf(rowId) < 0){
        value = selectedValue + "," +　rowId;
      } else {
        return false;
      }
    } else {
      value = rowId;
    }
    $("#pSids").text(value);
    var rowDatas = $("#dialog_form_table").jqGrid('getRowData', rowId);
    var addform = {"id":rowDatas.id,"name":rowDatas.name};
    sFormMap.push(addform);
  }

  function deletetSelectValue(rowId){
    for(var i=0; i<sFormMap.length; i++){
      if(rowId==sFormMap[i].id){
        sFormMap.splice(i,1);
        break;
      }
    }

    var selectedValue = $("#cKeys").text();
    var str = selectedValue.split(",");
    var selectedValueLast = "";
    for(var i = 0; i < str.length; i++){
      if(rowId != str[i]){
        selectedValueLast += str[i] + ",";
      }
    }
    selectedValueLast = selectedValueLast.substring(0, selectedValueLast.length-1);
    $("#cKeys").text(selectedValueLast);
  }

  function fn_dialog_forms_select(callback) {
    var formNames = "";
    var formIds = "";
    for(var i=0; i<sFormMap.length; i++){
      formNames += sFormMap[i].name + ",";
      formIds += sFormMap[i].id + ",";
    }
    formNames = formNames.substring(0,formNames.length-1);
    formIds = formIds.substring(0,formIds.length-1);
    callback({"names":formNames,"ids":formIds});
  }

</script>